package com.ys.service.base.impl;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.ys.dto.AddDto;
import com.ys.dto.EditDto;
import com.ys.entity.base.TbPlatform;
import com.ys.mapper.base.TbPlatformMapper;
import com.ys.service.base.TbPlatformService;
import com.ys.util.AESUtil;
import com.ys.vo.R;

@Service("tbPlatformService")
public class TbPlatformServiceImpl implements TbPlatformService {

	@Autowired
	private TbPlatformMapper tbPlatformMapper;

	/*
	 * 新增
	 */
	@Override
	public R add(Map<String, Object> params) throws Exception {
		
		TbPlatform entity = JSON.parseObject(JSON.toJSONString(params), TbPlatform.class);
		
		// 加密保存
		entity.setEncryptKey(AESUtil.aesEncrypt(entity.getEncryptKey()))
			.setEncryptSecret(AESUtil.aesEncrypt(entity.getEncryptSecret()))
			.setEncryptToken(AESUtil.aesEncrypt(entity.getEncryptToken()));

		tbPlatformMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	@Transactional
	public R edit(Map<String, Object> params) throws Exception {
		TbPlatform entity = JSON.parseObject(JSON.toJSONString(params), TbPlatform.class);
		
		// 加密保存
		entity.setEncryptKey(AESUtil.aesEncrypt(entity.getEncryptKey()))
			.setEncryptSecret(AESUtil.aesEncrypt(entity.getEncryptSecret()))
			.setEncryptToken(AESUtil.aesEncrypt(entity.getEncryptToken()));

		tbPlatformMapper.update(entity);
		return R.ok(EditDto.closeAndReload());
	}
}
